package com.wxw.BaseLibrary.net;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author david
 * @date 2020-05-18
 */
public class NetSpeedTimer {
    private long defaultDelay = 1000;
    private long defaultPeriod = 1000;
    private static final int ERROR_CODE = -101011010;
    private static NetSpeedTimer netSpeedTimer;
    private int mMsgWhat = ERROR_CODE;
    private NetSpeed mNetSpeed;
    private Context mContext;
    private SpeedTimerTask mSpeedTimerTask;
    private CopyOnWriteArrayList<NetSpeedStatusListener> listeners = new CopyOnWriteArrayList<>();

    public static final int NET_SPEED_TIMER_DEFAULT = 101010;

    private NetSpeedTimer(Context context) {
        this.mContext = context;
        this.mNetSpeed = new NetSpeed();
    }

    public static NetSpeedTimer getInstance(Context context) {
        if (netSpeedTimer == null) {
            netSpeedTimer = new NetSpeedTimer(context);
        }
        return netSpeedTimer;
    }

    public NetSpeedTimer setDelayTime(long delay) {
        this.defaultDelay = delay;
        return this;
    }

    public NetSpeedTimer setPeriodTime(long period) {
        this.defaultPeriod = period;
        return this;
    }

    public NetSpeedTimer setHandlerWhat(int what) {
        this.mMsgWhat = what;
        return this;
    }

    public NetSpeedTimer addListener(NetSpeedStatusListener listener) {
        if (listeners == null) {
            listeners = new CopyOnWriteArrayList<>();
        }
        if (listener != null) {
            listeners.add(listener);
        }
        return this;
    }

    public NetSpeedTimer removeListener(NetSpeedStatusListener listener) {
        if (listeners != null && listeners.contains(listener)) {
            listeners.remove(listener);
        }
        return this;
    }

    public void startSpeedTimer() {
        Timer timer = new Timer();
        mSpeedTimerTask = new SpeedTimerTask(mContext, mNetSpeed, mHandler, mMsgWhat);
        timer.schedule(mSpeedTimerTask, defaultDelay, defaultPeriod);

    }

    public void stopSpeedTimer() {
        if (null != mSpeedTimerTask) {
            mSpeedTimerTask.cancel();
            mSpeedTimerTask = null;
        }
    }

    public static void stopSpeed() {
        if (netSpeedTimer != null) {
            netSpeedTimer.stopSpeedTimer();
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == NetSpeedTimer.NET_SPEED_TIMER_DEFAULT) {
                if (listeners != null && listeners.size() > 0) {
                    for (NetSpeedStatusListener listener : listeners) {
                        listener.onNetSpeedChanged(msg, msg.obj.toString());
                    }
                }
            }
        }
    };

    private static class SpeedTimerTask extends TimerTask {
        private int mMsgWhat;
        private NetSpeed mNetSpeed;
        private Handler mHandler;
        private Context mContext;

        public SpeedTimerTask(Context context, NetSpeed netSpeed,
                              Handler handler, int what) {
            this.mContext = context;
            this.mNetSpeed = netSpeed;
            this.mHandler = handler;
            this.mMsgWhat = what;
        }

        @Override
        public void run() {
            if (null != mNetSpeed && null != mHandler) {
                Message obtainMessage = mHandler.obtainMessage();
                if (mMsgWhat != ERROR_CODE) {
                    obtainMessage.what = mMsgWhat;
                } else {
                    obtainMessage.what = NET_SPEED_TIMER_DEFAULT;
                }
                obtainMessage.obj = mNetSpeed.getNetSpeed(mContext.getApplicationInfo().uid);
                mHandler.sendMessage(obtainMessage);
            }
        }
    }

    public interface NetSpeedStatusListener {
        /**
         * 当网速变化时触发该事件
         * @param sender
         * @param netSpeedInfo
         */
        void onNetSpeedChanged(Object sender, String netSpeedInfo);
    }
}
